package com.bytedance.retrofit2.mime;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

public final class MultipartTypedOutput implements TypedOutput {
    public final List<MimePart> mimeParts = new LinkedList<>();
    private final byte[] footer;
    private final String boundary;
    private long length;

    public MultipartTypedOutput() {
        this(UUID.randomUUID().toString());
    }

    private MultipartTypedOutput(String boundary) {
        this.boundary = boundary;
        this.footer = buildBoundary(boundary, false, true);
        this.length = ((long) this.footer.length);
    }

    private static byte[] buildBoundary(String boundary, boolean first, boolean last) {
        try {
            StringBuilder v0_1 = new StringBuilder(boundary.length() + 8);
            if (!first) {
                v0_1.append("\r\n");
            }

            v0_1.append("--");
            v0_1.append(boundary);
            if (last) {
                v0_1.append("--");
            }

            v0_1.append("\r\n");
            return v0_1.toString().getBytes("UTF-8");
        } catch (IOException v0) {
            throw new RuntimeException("Unable to write multipart boundary", ((Throwable) v0));
        }
    }

    static byte[] buildBoundary(String boundary, boolean first) {
        return buildBoundary(boundary, first, false);
    }

    private static byte[] buildHeader(String name, String transferEncoding, TypedOutput value) {
        try {
            StringBuilder sb = new StringBuilder(128);
            sb.append("Content-Disposition: form-data; name=\"");
            sb.append(name);
            String fileName = value.fileName();
            if (fileName != null) {
                sb.append("\"; filename=\"");
                sb.append(fileName);
            }

            sb.append("\"\r\nContent-Type: ");
            sb.append(value.mimeType());
            long contentLength = value.length();
            if (contentLength != -1) {
                sb.append("\r\nContent-Length: ").append(contentLength);
            }

            sb.append("\r\nContent-Transfer-Encoding: ");
            sb.append(transferEncoding);
            sb.append("\r\n\r\n");
            return sb.toString().getBytes("UTF-8");
        } catch (IOException e) {
            throw new RuntimeException("Unable to write multipart header", e);
        }
    }

    public final String mimeType() {
        return "multipart/form-data; boundary=" + this.boundary;
    }

    public final void writeTo(OutputStream out) throws IOException {
        Iterator v1 = this.mimeParts.iterator();
        while (v1.hasNext()) {
            Object v0 = v1.next();
            ((MimePart) v0).build();
            out.write(((MimePart) v0).partBoundary);
            out.write(((MimePart) v0).partHeader);
            ((MimePart) v0).body.writeTo(out);
        }

        out.write(this.footer);
    }

    public final void a(String arg9, String arg10, TypedOutput arg11) {
        long v6 = -1;
        if (arg9 == null) {
            throw new NullPointerException("Part name must not be null.");
        }

        if (arg10 == null) {
            throw new NullPointerException("Transfer encoding must not be null.");
        }

        if (arg11 == null) {
            throw new NullPointerException("Part body must not be null.");
        }

        MimePart v0 = new MimePart(arg9, arg10, arg11, this.boundary, this.mimeParts.isEmpty());
        this.mimeParts.add(v0);
        v0.build();
        long v0_1 = v0.body.length() > v6 ? (((long) v0.partHeader.length)) + (v0.body.length() + (((long) v0.partBoundary.length))) : v6;
        if (v0_1 == v6) {
            this.length = v6;
        } else if (this.length != v6) {
            this.length = v0_1 + this.length;
        }
    }

    public final long length() {
        return this.length;
    }

    public final String fileName() {
        return null;
    }

    static final class MimePart {
        final TypedOutput body;
        private final String name;
        private final String e;
        private final boolean isFirst;
        private final String boundary;
        byte[] partBoundary;
        byte[] partHeader;
        private boolean isBuilt;

        public MimePart(String name, String arg2, TypedOutput arg3, String boundary, boolean isFirst) {
            this.name = name;
            this.e = arg2;
            this.body = arg3;
            this.isFirst = isFirst;
            this.boundary = boundary;
        }

        final void build() {
            if (!this.isBuilt) {
                this.partBoundary = MultipartTypedOutput.buildBoundary(this.boundary, this.isFirst);
                this.partHeader = buildHeader(this.name, this.e, this.body);
                this.isBuilt = true;
            }
        }
    }
}

